package com.ssy.lingxi.member.merchant.repository;

import com.ssy.lingxi.member.merchant.entity.MemberRelationDO;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 会员关系操作
 * @author 万宁
 * @version 2.0.0
 * @date 2020-08-18
 */
@Repository
public interface MemberRelationRepository extends JpaRepository<MemberRelationDO, Long>, JpaSpecificationExecutor<MemberRelationDO> {

    List<MemberRelationDO> findByMemberIdAndRoleId(Long upperMemberId, Long upperRoleId);

    List<MemberRelationDO> findBySubMemberIdAndSubRoleId(Long subMemberId, Long subRoleId);

    List<MemberRelationDO> findBySubMemberIdAndSubRoleIdAndSubMemberLevelTypeEnumNot(Long subMemberId, Long subRoleId,Integer levelTypeEnum);

    List<MemberRelationDO> findBySubMemberIdAndSubRoleIdAndVerified(Long subMemberId, Long subRoleId, Integer verified);

    Page<MemberRelationDO> findBySubMemberIdAndSubRoleId(Long subMemberId, Long subRoleId, Pageable pageable);

    Page<MemberRelationDO> findBySubMemberIdAndSubRoleIdAndRelType(Long subMemberId, Long subRoleId, Integer relType, Pageable pageable);

    Page<MemberRelationDO> findBySubMemberIdAndSubRoleIdAndRelTypeAndVerified(Long subMemberId, Long subRoleId, Integer relType, Integer verified, Pageable pageable);

    List<MemberRelationDO> findBySubMemberIdAndSubRoleIdAndRelType(Long subMemberId, Long subRoleId, Integer relType);

    MemberRelationDO findFirstByMemberIdAndRoleIdAndSubMemberIdAndSubRoleId(Long memberId, Long roleId, Long subMemberId, Long subRoleId);

    List<MemberRelationDO> findAllByRelType(Integer relType);

    List<MemberRelationDO> findByRelTypeAndSubMemberIdIn(Integer relType, List<Long> subMemberIds);

    MemberRelationDO findFirstBySubMemberIdAndSubRoleIdAndRelType(Long subMemberId, Long subRoleId, Integer relType);

    MemberRelationDO findFirstBySubMemberIdAndSubRoleIdAndRelTypeAndVerified(Long subMemberId, Long subRoleId, Integer relType, Integer verified);

    List<MemberRelationDO> findAllBySubMemberIdInAndRelType(List<Long> subMemberId, Integer relType);

    List<MemberRelationDO> findBySubMemberIdAndRelType(Long subMemberId, Integer relType);

    boolean existsBySubMemberIdAndSubRoleIdAndRelType(Long subMemberId, Long subRoleId, Integer relType);

    boolean existsBySubMemberIdAndSubRoleIdAndRelTypeAndIdNot(Long subMemberId, Long subRoleId, Integer relType, Long id);

    boolean existsByMemberIdAndRoleIdAndSubMemberIdAndSubRoleId(Long memberId, Long roleId, Long subMemberId, Long subRoleId);

    @Transactional
    void deleteAllBySubMemberIdAndSubRoleId(Long subMemberId, Long subRoleId);

    MemberRelationDO findFirstByRelType(Integer relType);

    List<MemberRelationDO> findAllByMemberIdAndRoleIdAndRelType(Long memberId, Long memberRoleId, Integer relType);

    boolean existsBySubRoleIdAndRelType(Long subRoleId, Integer relType);
}
